import { HandlerContext, TransactionContext, Transaction, GetApi, OrmEntities } from '@dbos-inc/dbos-sdk';
import { EntityManager } from "typeorm";
import { DBOSHello } from '../entities/DBOSHello';

@OrmEntities([DBOSHello])
export class Hello {

  @GetApi('/') // Serve a quick readme for the app
  static async readme(_ctxt: HandlerContext) {
    const readme = `<html><body><p>
          Welcome to the DBOS Hello App!<br><br>
          Visit the route /greeting/:name to be greeted!<br>
          For example, visit <a href="/greeting/dbos">/greeting/dbos</a>.<br>
          The counter increments with each page visit.<br>
          </p></body></html>`;
    return Promise.resolve(readme);
  }

  @GetApi('/greeting/:name')
  @Transaction()
  static async helloTransaction(txnCtxt: TransactionContext<EntityManager>, name: string) {
    const greeting = `Hello, ${name}!`;
    let entity = new DBOSHello();
    entity.greeting = greeting;
    entity = await txnCtxt.client.save(entity);
    return `Greeting ${entity.greeting_id}: ${greeting}`;
  }
}
